<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：通用控制器。
 *
 **/
namespace Api\Controller;
use Think\Controller\RestController;

class ComController extends RestController
{
    
    protected $res=array(
        'code'=>200,
        'msg'=>'ok',
        'data'=>array()
    );
    
    protected $user;
    
    /**
     * 用户登录校验
     */
    public function _initialize(){
        C(setting());
        header('Content-Type:application/json; charset=utf-8');
        $token = I('server.HTTP_TOKEN');
        if(!$token){
            $token = I('token');
        }
        if(!$token){
            $this->res['code'] = 401;
            $this->res['msg'] = 'invalid token';
            $this->response($this->res,'json');
        }
        //验证token是否过期
        $user_token = M('user_token')->where(array('token'=>$token))->find();
        if(!$user_token){
            $this->res['code'] = 401;
            $this->res['msg'] = 'invalid token';
            $this->response($this->res,'json');
        }
        if($user_token['expire_t'] < time()){
            $this->res['code'] = 401;
            $this->res['msg'] = 'invalid token';
            $this->response($this->res,'json');
        }
        $this->user = M('user')
                            ->alias('a')
                            ->join('LEFT JOIN '.C('DB_PREFIX').'user_token b on a.id=b.uid')
                            ->field('id,username,head_img,nickname,sex,status,expri_day,level_id,money,int_code,team_level')
                            ->where(array('b.token'=>$token))
                            ->find();
        if($this->user['status'] == 0){
            $this->res['code'] = 401;
            $this->res['msg'] = '该账户已被管理员禁止，请联系管理员！';
            $this->response($this->res,'json');
        }
        $this->user['token'] = $token;
        $this->user['head_img'] = C('URL').$this->user['head_img'];
        // 更新最近登录时间
        M('user')->data(array('login_t'=>time()))->where('id='.$this->user['id'])->save();
    }
}